// 用户账单查询相关

const res = require('express/lib/response')
const user = require('../sql/index')
const bill = require('../sql/bill')

const sql = `SELECT userAccount,billType,billAmount,billYear,billMouth,billDay,userBalance,userPlateNumber from peoject_bill.userbill WHERE `
// 按余额操作时间查询
// 数据应包含 起始年份 月份 日 终止年份 月份 日
exports.queryByTime = (req,res) =>{
  const userinfo = req.body

  const timeSql = sql+`userAccount=? and billYear>=? and billMouth>=? and billDay>=? and billYear<=? and billMouth <=? and billDay <=?`
  
  bill.query(timeSql,[
    userinfo.userAccount,
    userinfo.beginYear,
    userinfo.beginMouth,
    userinfo.beginDay,
    userinfo.finalYear,
    userinfo.finalMouth,
    userinfo.finalDay,],(err,results)=>{
      if(err) return res.cc(err)

      res.send(results)
    })
}

//默认查询
exports.queryByDefault = (req,res) =>{
  const userinfo = req.body
  const defaultSql = sql + `userAccount=?`

  bill.query(defaultSql,userinfo.userAccount,(err,results)=>{
      if(err) return res.cc(err)

      res.send(results)
    })
}

// 按余额操作类型查询
exports.queryByType = (req,res) =>{
  const userinfo = req.body
  const TypeSql = sql + `userAccount=? and billType=?`

  console.log(userinfo.userAccount);
  console.log(userinfo.billType);
  console.log(TypeSql);
  bill.query(TypeSql,[userinfo.userAccount,userinfo.billType],(err,results)=>{
    if(err) return res.cc(err)

    res.send(results)
  })
}

// 按车牌号查询
exports.queryByPlate = (req,res) =>{
  const userinfo = req.body
  let PlateSql = sql + `userAccount=? and userPlateNumber=?`

  if(userinfo.limit != undefined)
    PlateSql += " limit "+userinfo.limit

  bill.query(PlateSql,[userinfo.userAccount,userinfo.plateNumber],(err,results)=>{
    if(err) return res.cc(err)

    res.send(results)
  })
}